package cn.com.digitalnet.ad.aop;

import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.com.digitalnet.ad.entity.User;
import cn.com.digitalnet.ad.security.ShiroUser;
import cn.com.digitalnet.ad.service.UserService;
import cn.com.digitalnet.ad.util.Holder;

/**
 * 普通controller方法前的增强，尝试将已登录的user放到Holder中，但并不做权限检查
 * @author qidafang
 */
@Component
public class UserAspect {
	@Autowired
	private UserService userService;
	
	public Object doAround(ProceedingJoinPoint p) throws Throwable{
		User user = null;
		//尝试获得已登录的用户
		ShiroUser shiroUser = (ShiroUser)SecurityUtils.getSubject().getPrincipal();
		if(shiroUser!=null){
			Long userId = shiroUser.id;
			if(userId != null){
				user = userService.findOne(userId);
			}
		}
		if(user!=null){
			Holder.user.set(user);
		}
		return p.proceed();
	}
}
